﻿/*
 *  ChgDoc CMS - GNU General Public License, version 3 (GPL-3.0)
 *  Copyright (C)  2012. Ian Quigley
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SqliteORM;
using ChgDoc.Entity;

namespace ChgDoc.Entity
{
    [Table]
    public class Article : TableBase<Article>
    {
        [PrimaryKey( true )]
        public long Id { get; set; }

        [Field]
        public string Title { get; set; }

        [Field]
        public long TemplateId { get; set; }

        [Field]
        public string Body { get; set; }
        internal static List<Article> ReadAll()
        {
            return Get( ad => ad.Select() ).ToList();
        }

        internal static void Delete(long id)
        {
            Do(ad => ad.Delete(id));
        }

        internal static Article Read( long id )
        {
            return Get( ad => ad.Read( id ) );
        }


        internal IEnumerable<Location> Locations()
        {
            using (var adapter = TableAdapter<Mash<ArticleLocation, Location>>.Open())
            {
                return from mash in adapter.Select().Where(Where.Equal("t1.ArticleId", Id)) select mash.Table2;
            }
        }

        internal void AddLocation( long locationId )
        {
            ArticleLocation.Add( Id, locationId );
        }

        internal void DeleteLocation( long locationId )
        {
            ArticleLocation.Delete( Id, locationId );
        }

        internal IEnumerable<Tag> Tags()
        {
            using (var adapter = TableAdapter<Mash<ArticleTag, Tag>>.Open())
            {
                return from mash in adapter.Select().Where(Where.Equal("t1.ArticleId", Id)) select mash.Table2;
            }
        }
        internal void AddTag(long TagId)
        {
            ArticleTag.Add(Id, TagId);
        }

        internal void DeleteTag(long TagId)
        {
            ArticleTag.Delete(Id, TagId);
        }
        
        internal void DeletePageResponse(long responseId)
        {
            ArticlePageResponse.Delete(Id, responseId);
            PageResponse.Delete(responseId);
        }

        internal void AddPageResponse(long responseId)
        {
            ArticlePageResponse.Add(Id, responseId);
        }

        internal List<PageResponse> Urls()
        {
            using (var adapater = TableAdapter<Mash<ArticlePageResponse, PageResponse>>.Open())
            {
                return (from mash in adapater.Select().Where(Where.Equal("t1.ArticleId", this.Id)) select mash.Table2).ToList();
            }
        }

        internal List<EntityAttribute> Attributes()
        {
            return EntityAttribute.ReadAll(EntityType.Article, this.Id);
        }

        internal void Attribute(string code, string value)
        {
            var attribute = EntityAttribute.Find( EntityType.Article, this.Id, code );
            if (attribute == null)
                attribute = new EntityAttribute();

            attribute.Entity = EntityType.Article;
            attribute.EntityId = this.Id;
            attribute.Code = code;
            attribute.Value = value;                

            attribute.Save();
        }

        public List<string> DistinctAttributeCodes()
        {
            return EntityAttribute.DistinctCodesFor(EntityType.Article);
        }
    }
}